package de.fhin.mas.agents.manager.gui;

import jade.core.Agent;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import de.fhin.mas.agents.manager.ManagerAgent;
import de.fhin.mas.agents.manager.data.LocationData;
import de.fhin.mas.agents.manager.data.ObjectData;
import de.fhin.mas.ontology.Location;
import de.fhin.mas.ontology.SearchableObject;

public class MASGui extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JLabel ImageLabel;
	private JLabel Header1;
	private JLabel Header2;
	private JLabel textOrder;
	private JLabel textObjects;
	private JLabel textTargets;
	private JLabel Messages;
	private JLabel textStop;
	private JLabel textReason;
	private JLabel textAbort;
	private JLabel textAbortTarget;
	private JLabel line1;
	private JLabel line2;
	private JLabel line3;

	private JList objects;
	private JList targets;
	private JList targetsAbort;

	private JButton OrderButton;
	private JButton SystemStopButton;
	private JButton ResumeButton;
	private JButton AbortButton;

	private JTextField SystemStopReason;

	private JTextArea MessageArea;

	private JScrollPane targetsScroll;
	private JScrollPane targetsAbortScroll;
	private JScrollPane objectsScroll;
	private JScrollPane MessageAreaScroll;

	private Container cont;

	private final ManagerAgent managerAgent;

	/**
	 * This is the default constructor
	 */
	public MASGui(Agent a) {
		super();
		managerAgent = (ManagerAgent) a;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		setSize(700, 850);
		setTitle("MAS");
		setResizable(false);

		// Erste Zeile mit Bild und Text
		ImageLabel = new JLabel(new ImageIcon("src/main/Mas.png"));
		ImageLabel.setBounds(5, 5, 135, 135);

		Header1 = new JLabel("Multi Agent System");
		Header1.setFont(new Font("Dialog", 0, 50));
		Header1.setBounds(200, 20, 500, 60);

		Header2 = new JLabel("Hochschule Ingolstadt");
		Header2.setFont(new Font("Dialog", 0, 30));
		Header2.setBounds(200, 80, 500, 60);

		// Abschnitt mit New Order
		textOrder = new JLabel("New Order:");
		textOrder.setFont(new Font("Dialog", 0, 15));
		textOrder.setBounds(5, 170, 700, 20);

		textObjects = new JLabel("Objects:");
		textObjects.setBounds(20, 200, 700, 20);

		textTargets = new JLabel("Targets:");
		textTargets.setBounds(300, 200, 700, 20);

		objects = new JList(ObjectData.getObjectData());
		objects.setBounds(20, 225, 230, 100);
		objects.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		objectsScroll = new JScrollPane(objects);
		objectsScroll.setBounds(new Rectangle(20, 225, 230, 100));

		targets = new JList(LocationData.getStorageData());
		targets.setBounds(300, 225, 230, 100);
		targets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		targetsScroll = new JScrollPane(targets);
		targetsScroll.setBounds(new Rectangle(300, 225, 230, 100));

		OrderButton = new JButton("Start Order");
		OrderButton.setBounds(560, 295, 110, 30);
		OrderButton.addActionListener(this);

		line1 = new JLabel();
		line1.setBorder(BorderFactory.createLineBorder(Color.gray));
		line1.setBounds(0, 340, 700, 1);

		// Abschnitt mit System Stop
		textStop = new JLabel("System Stop Required?:");
		textStop.setFont(new Font("Dialog", 0, 15));
		textStop.setBounds(5, 355, 700, 20);

		textReason = new JLabel("Reason for System Stop:");
		textReason.setBounds(20, 380, 150, 20);

		SystemStopReason = new JTextField("[default]");
		SystemStopReason.setBounds(200, 380, 300, 20);
		SystemStopReason.setEnabled(false);

		SystemStopButton = new JButton("System Stop");
		SystemStopButton.setBounds(520, 380, 150, 30);
		SystemStopButton.addActionListener(this);
		SystemStopButton.setEnabled(false);

		ResumeButton = new JButton("Resume System");
		ResumeButton.setBounds(520, 420, 150, 30);
		ResumeButton.addActionListener(this);
		ResumeButton.setEnabled(false);

		line2 = new JLabel();
		line2.setBorder(BorderFactory.createLineBorder(Color.gray));
		line2.setBounds(0, 465, 700, 1);

		// Abschnitt mit Abort and go to
		textAbort = new JLabel("Abort and go to:");
		textAbort.setFont(new Font("Dialog", 0, 15));
		textAbort.setBounds(5, 480, 110, 20);

		textAbortTarget = new JLabel("New Target:");
		textAbortTarget.setBounds(20, 505, 100, 20);

		targetsAbort = new JList(LocationData.getStorageData());
		targetsAbort.setBounds(200, 505, 250, 100);
		targetsAbort.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		targetsAbort.setEnabled(false);
		targetsAbortScroll = new JScrollPane(targetsAbort);
		targetsAbortScroll.setBounds(new Rectangle(200, 505, 250, 100));

		AbortButton = new JButton("Abort and Go to");
		AbortButton.setBounds(520, 575, 150, 30);
		AbortButton.addActionListener(this);
		AbortButton.setEnabled(false);

		line3 = new JLabel();
		line3.setBorder(BorderFactory.createLineBorder(Color.gray));
		line3.setBounds(0, 620, 700, 1);

		// Abschnitt mit Message Box
		Messages = new JLabel("Messages:");
		Messages.setFont(new Font("Dialog", 0, 15));
		Messages.setBounds(5, 635, 150, 20);

		MessageArea = new JTextArea(4, 10);
		MessageArea.setEditable(false);
		MessageArea.setBounds(20, 660, 650, 130);
		MessageArea.setLineWrap(true);
		MessageAreaScroll = new JScrollPane(MessageArea);
		MessageAreaScroll.setBounds(new Rectangle(20, 660, 650, 130));

		cont = getContentPane();
		cont.setLayout(null);
		cont.add(ImageLabel);
		cont.add(Header1);
		cont.add(Header2);
		cont.add(textOrder);
		cont.add(textObjects);
		cont.add(textTargets);
		cont.add(objectsScroll);
		cont.add(targetsScroll);
		cont.add(OrderButton);
		cont.add(line1);
		cont.add(textStop);
		cont.add(textReason);
		cont.add(SystemStopReason);
		cont.add(SystemStopButton);
		cont.add(ResumeButton);
		cont.add(line2);
		cont.add(textAbort);
		cont.add(textAbortTarget);
		cont.add(targetsAbortScroll);
		cont.add(AbortButton);
		cont.add(line3);
		cont.add(Messages);
		cont.add(MessageAreaScroll);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == OrderButton) {
			if (objects.isSelectionEmpty() || targets.isSelectionEmpty())
				setMessage("Please select object and target!");
			else {
				SearchableObject obj = (SearchableObject) objects
						.getSelectedValue();
				setMessage(managerAgent.getLocalName()
						+ ": You choose the object  '" + obj.toString()
						+ "'  with the Index  '" + objects.getSelectedIndex()
						+ "'  in 'object_data'");

				Location loc = (Location) targets.getSelectedValue();
				setMessage(managerAgent.getLocalName()
						+ ": You choose the target  '" + loc.toString()
						+ "'  with the Index  '" + targets.getSelectedIndex()
						+ "'  in 'storage_data'");

				// Neuen Auftrag initialisieren
				managerAgent.initNewOrder(obj, loc);

				// Buttons und Felder aktivieren
				SystemStopReason.setEnabled(true);
				SystemStopButton.setEnabled(true);
				targetsAbort.setEnabled(true);
				AbortButton.setEnabled(true);
			}
		} else

		// System Stop Button
		if (e.getSource() == SystemStopButton) {
			setMessage(managerAgent.getLocalName()
					+ ": System Stop Button pressed");

			if (SystemStopReason.getText().equals("")) {
				setMessage(managerAgent.getLocalName()
						+ ": Add a reason for system stop");
			} else {
				SystemStopButton.setEnabled(false);
				ResumeButton.setEnabled(true);

				managerAgent.initSystemStop(SystemStopReason.getText());
			}
		}

		// Resume Button
		else if (e.getSource() == ResumeButton) {
			setMessage(managerAgent.getLocalName() + ": Resume Button pressed");

			SystemStopButton.setEnabled(true);
			ResumeButton.setEnabled(false);

			managerAgent.initResume();
		}

		// Abort and Go to Button
		else if (e.getSource() == AbortButton) {
			if (targetsAbort.isSelectionEmpty()) {
				setMessage(managerAgent.getLocalName()
						+ ": Choose an new Target or 'no target'");
			} else {
				Location loc = (Location) targetsAbort.getSelectedValue();
				setMessage(managerAgent.getLocalName()
						+ ": Abort Button pressed. new target: "
						+ loc.toString());

				managerAgent.initAbortAndGoto(loc);
			}
		}
	}

	/**
	 * Methode hängt eine neue Message an die Message TextArea der GUI
	 * 
	 * @param Message
	 *            Stringvariable welche hinzugefügt werden soll
	 */
	public void setMessage(String Message) {
		MessageArea.append(LogDate.getTime() + "\t" + Message + "\n");

	}

}
